
            <!DOCTYPE html>
            <html lang="en">
            <head>
                <meta charset="UTF-8">
                <title>[k8s] 第十章 DashBoard</title>
            </head>
            <body>
            <a href="https://andyoung.blog.csdn.net">原作者博客</a>
            <div id="content_views" class="markdown_views prism-atom-one-light">
                    <svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
                        <path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
                    </svg>
                    <h2><a id="_DashBoard_0"></a>第十章 DashBoard</h2> 
<p>​ 之前在kubernetes中完成的所有操作都是通过命令行工具kubectl完成的。其实，为了提供更丰富的用户体验，kubernetes还开发了一个基于web的用户界面（Dashboard）。用户可以使用Dashboard部署容器化的应用，还可以监控应用的状态，执行故障排查以及管理kubernetes中各种资源。</p> 
<h3><a id="Dashboard_4"></a>部署Dashboard</h3> 
<ol><li>下载yaml，并运行Dashboard</li></ol> 
<pre><code class="prism language-powershell"><span class="token comment"># 下载yaml</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># wget  https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0/aio/deploy/recommended.yaml</span>

<span class="token comment"># 修改kubernetes-dashboard的Service类型</span>
kind: Service
apiVersion: v1
metadata:
  labels:
    k8s<span class="token operator">-</span>app: kubernetes<span class="token operator">-</span>dashboard
  name: kubernetes<span class="token operator">-</span>dashboard
  namespace: kubernetes<span class="token operator">-</span>dashboard
spec:
  <span class="token function">type</span>: NodePort  <span class="token comment"># 新增</span>
  ports:
    <span class="token operator">-</span> port: 443
      targetPort: 8443
      nodePort: 30009  <span class="token comment"># 新增</span>
  selector:
    k8s<span class="token operator">-</span>app: kubernetes<span class="token operator">-</span>dashboard

<span class="token comment"># 部署</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl create -f recommended.yaml</span>

<span class="token comment"># 查看namespace下的kubernetes-dashboard下的资源</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl get pod,svc -n kubernetes-dashboard</span>
NAME                                            READY   STATUS    RESTARTS   AGE
pod<span class="token operator">/</span>dashboard<span class="token operator">-</span>metrics<span class="token operator">-</span>scraper<span class="token operator">-</span>c79c65bb7<span class="token operator">-</span>zwfvw   1<span class="token operator">/</span>1     Running   0          111s
pod<span class="token operator">/</span>kubernetes<span class="token operator">-</span>dashboard<span class="token operator">-</span>56484d4c5<span class="token operator">-</span>z95z5        1<span class="token operator">/</span>1     Running   0          111s

NAME                               <span class="token function">TYPE</span>       CLUSTER<span class="token operator">-</span>IP      EXTERNAL<span class="token operator">-</span>IP  PORT<span class="token punctuation">(</span>S<span class="token punctuation">)</span>         AGE
service<span class="token operator">/</span>dashboard<span class="token operator">-</span>metrics<span class="token operator">-</span>scraper  ClusterIP  10<span class="token punctuation">.</span>96<span class="token punctuation">.</span>89<span class="token punctuation">.</span>218    &lt;none&gt;       8000<span class="token operator">/</span>TCP        111s
service<span class="token operator">/</span>kubernetes<span class="token operator">-</span>dashboard       NodePort   10<span class="token punctuation">.</span>104<span class="token punctuation">.</span>178<span class="token punctuation">.</span>171  &lt;none&gt;       443:30009<span class="token operator">/</span>TCP   111s
</code></pre> 
<p>2）创建访问账户，获取token</p> 
<pre><code class="prism language-powershell"><span class="token comment"># 创建账号</span>
<span class="token namespace">[root@master-1 ~]</span><span class="token comment"># kubectl create serviceaccount dashboard-admin -n kubernetes-dashboard</span>

<span class="token comment"># 授权</span>
<span class="token namespace">[root@master-1 ~]</span><span class="token comment"># kubectl create clusterrolebinding dashboard-admin-rb --clusterrole=cluster-admin --serviceaccount=kubernetes-dashboard:dashboard-admin</span>

<span class="token comment"># 获取账号token</span>
<span class="token namespace">[root@master ~]</span><span class="token comment">#  kubectl get secrets -n kubernetes-dashboard | grep dashboard-admin</span>
dashboard<span class="token operator">-</span>admin<span class="token operator">-</span>token<span class="token operator">-</span>xbqhh        kubernetes<span class="token punctuation">.</span>io<span class="token operator">/</span>service<span class="token operator">-</span>account<span class="token operator">-</span>token   3      2m35s

<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl describe secrets dashboard-admin-token-xbqhh -n kubernetes-dashboard</span>
Name:         dashboard<span class="token operator">-</span>admin<span class="token operator">-</span>token<span class="token operator">-</span>xbqhh
Namespace:    kubernetes<span class="token operator">-</span>dashboard
Labels:       &lt;none&gt;
Annotations:  kubernetes<span class="token punctuation">.</span>io<span class="token operator">/</span>service<span class="token operator">-</span>account<span class="token punctuation">.</span>name: dashboard<span class="token operator">-</span>admin
              kubernetes<span class="token punctuation">.</span>io<span class="token operator">/</span>service<span class="token operator">-</span>account<span class="token punctuation">.</span>uid: 95d84d80<span class="token operator">-</span>be7a<span class="token operator">-</span>4d10<span class="token operator">-</span>a2e0<span class="token operator">-</span>68f90222d039

<span class="token function">Type</span>:  kubernetes<span class="token punctuation">.</span>io<span class="token operator">/</span>service<span class="token operator">-</span>account<span class="token operator">-</span>token

<span class="token keyword">Data</span>
====
namespace:  20 bytes
token:      eyJhbGciOiJSUzI1NiIsImtpZCI6ImJrYkF4bW5XcDhWcmNGUGJtek5NODFuSXl1aWptMmU2M3o4LTY5a2FKS2cifQ<span class="token punctuation">.</span>eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJkYXNoYm9hcmQtYWRtaW4tdG9rZW4teGJxaGgiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoiZGFzaGJvYXJkLWFkbWluIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiOTVkODRkODAtYmU3YS00ZDEwLWEyZTAtNjhmOTAyMjJkMDM5Iiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Omt1YmVybmV0ZXMtZGFzaGJvYXJkOmRhc2hib2FyZC1hZG1pbiJ9<span class="token punctuation">.</span>NAl7e8ZfWWdDoPxkqzJzTB46sK9E8iuJYnUI9vnBaY3Jts7T1g1msjsBnbxzQSYgAG-<span class="token operator">-</span>cV0WYxjndzJY_UWCwaGPrQrt_GunxmOK9AUnzURqm55GR2RXIZtjsWVP2EBatsDgHRmuUbQvTFOvdJB4x3nXcYLN2opAaMqg3rnU2rr<span class="token operator">-</span>A8zCrIuX_eca12wIp_QiuP3SF<span class="token operator">-</span>tzpdLpsyRfegTJZl6YnSGyaVkC9id<span class="token operator">-</span>cxZRb307qdCfXPfCHR_2rt5FVfxARgg_C0e3eFHaaYQO7CitxsnIoIXpOFNAR8aUrmopJyODQIPqBWUehb7FhlU1DCduHnIIXVC_UICZ<span class="token operator">-</span>MKYewBDLw
ca<span class="token punctuation">.</span>crt:     1025 bytes
</code></pre> 
<p>3）通过浏览器访问Dashboard的UI</p> 
<h5><a id="token_73"></a>以token方式登录</h5> 
<p>在登录页面上输入上面的token</p> 
<img src="https://i-blog.csdnimg.cn/blog_migrate/d8cd1f753239a8b6180e4781f2391ebc.png" alt="image-20200520144548997"> 
<p>出现下面的页面代表成功</p> 
<img src="https://i-blog.csdnimg.cn/blog_migrate/8c60c8a69c5cda1a650092fb0453399d.png" alt="image-20200520144959353"> #### 以 Kubeconfig 方式登录 &gt; dashboard-admin-token-xbqhh 是上方生成的 secrets 
<pre><code>##  获取token
DASH_TOCKEN=$(kubectl -n kubernetes-dashboard get secrets dashboard-admin-token-xbqhh -o jsonpath={.data.token} |base64 -d)

## 设置 kubeconfig 文件中的一个集群条目  --server 自备
kubectl config set-cluster kubernetes --server="https://192.168.0.183:6443" --kubeconfig=/root/dashbord-admin.conf

## 设置 kubeconfig 文件中的一个用户条目
kubectl config set-credentials kubernetes-dashboard --token=$DASH_TOCKEN --kubeconfig=/root/dashbord-admin.conf

## 设置 kubeconfig 文件中的一个上下文条目
kubectl config set-context kubernetes-dashboard@kubernetes --cluster=kubernetes --user=kubernetes-dashboard --kubeconfig=/root/dashbord-admin.conf

## 设置 kubeconfig 文件中的当前上下文
kubectl config use-context kubernetes-dashboard@kubernetes --kubeconfig=/root/dashbord-admin.conf
</code></pre> 
<p>下载当前文件到本地 <code>/root/dashbord-admin.conf</code></p> 
<h6><a id="_dashbordadminconf__101"></a>使用 dashbord-admin.conf 登录</h6> 
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/230fad347b4339533c1cff2ed91f8a93.png" alt="在这里插入图片描述"></p> 
<h3><a id="DashBoard_104"></a>使用DashBoard</h3> 
<p>本章节以Deployment为例演示DashBoard的使用</p> 
<p><strong>查看</strong></p> 
<p>选择指定的命名空间<code>dev</code>，然后点击<code>Deployments</code>，查看dev空间下的所有deployment</p> 
<img src="https://i-blog.csdnimg.cn/blog_migrate/9664aabe9224af5ccbbf17e43ac186b8.png"> 
<p><strong>扩缩容</strong></p> 
<p>在<code>Deployment</code>上点击<code>规模</code>，然后指定<code>目标副本数量</code>，点击确定</p> 
<img src="https://i-blog.csdnimg.cn/blog_migrate/82cb802a8f1f3241bbda4840d8954ffe.png"> 
<p><strong>编辑</strong></p> 
<p>在<code>Deployment</code>上点击<code>编辑</code>，然后修改<code>yaml文件</code>，点击确定</p> 
<img src="https://i-blog.csdnimg.cn/blog_migrate/a7ad65252b617193926fd1f5e9c19904.png" alt="image-20200520163253644"> 
<p><strong>查看Pod</strong></p> 
<p>点击<code>Pods</code>, 查看pods列表</p> 
<img src="https://i-blog.csdnimg.cn/blog_migrate/fee79ac7c64613bd450b31a75617dd3f.png"> 
<p><strong>操作Pod</strong></p> 
<p>选中某个Pod，可以对其执行日志（logs）、进入执行（exec）、编辑、删除操作</p> 
<img src="https://i-blog.csdnimg.cn/blog_migrate/4ebcf7f30f373e6b61c24aeec9d82fc6.png"> 
<blockquote> 
 <p>Dashboard提供了kubectl的绝大部分功能，这里不再一一演示</p> 
</blockquote>
                </div>
            </body>
            </html>
            